package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SettingManager {

    @DubboReference
    QuestionService questionService;

    @DubboReference
    NotificationService notificationService;

    @DubboReference
    BlackListService blackListService;

    @DubboReference
    UserInfoService userInfoService;

    public ResponseEntity findSetting() {
        //0.获取当前登陆者
        User user = UserHolder.get();

        //1.远程调用QuestionService查询用户问题
        Question question = questionService.findByUserId(user.getId());

        //2.远程调用NotificationService查询用户通知设置
        Notification notification = notificationService.findByUserId(user.getId());

        //3.创建vo对象,封装vo
        SettingVo vo = new SettingVo();

        vo.setId(user.getId());
        vo.setPhone(user.getMobile());

        if (question!=null) {
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }

        if (notification!=null) {
            vo.setLikeNotification(notification.getLikeNotification());
            vo.setPinglunNotification(notification.getPinglunNotification());
            vo.setGonggaoNotification(notification.getGonggaoNotification());
        }

        //4.返回vo
        return ResponseEntity.ok(vo);
    }

    public void setQuestion(String content) {
        //1.获取用户id
        Long userId = UserHolder.get().getId();

        //2.远程调用service查询用户问题
        Question question = questionService.findByUserId(userId);

        if (question == null) {
            //2.1若问题不存在,新增
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);

            questionService.save(question);
        }else{
            //2.2若问题存在,更新
            question.setStrangerQuestion(content);

            questionService.update(question);
        }
    }

    public void setNotification(Notification param) {
        //1.获取用户id
        Long userId = UserHolder.get().getId();

        //2.获取之前的设置
        Notification notification = notificationService.findByUserId(userId);

        if (notification == null) {
            //2.1若没有设置过,新增
            param.setUserId(userId);
            notificationService.save(param);
        }else{
            //2.2若设置过,更新
            param.setId(notification.getId());
            notificationService.update(param);
        }
    }

    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {
        //1.获取用户id
        Long userId = UserHolder.get().getId();

        //2.调用service返回pageBeanVo(里面的集合只有黑名单用户id)
        PageBeanVo vo = blackListService.findByPage(pageNum, pageSize, userId);

        //3.获取vo中的集合
        List<BlackList> items = (List<BlackList>) vo.getItems();

        //4.遍历集合获取每个黑名单id,调用userInfoService查询userInfo,将每个userInfo放入userInfoList中
        List<UserInfo> userInfoList=new ArrayList<>();

        if (CollUtil.isNotEmpty(items)) {
            items.forEach(bl->{
                //获取黑名单用户id,调用userInfoService查询userInfo
                UserInfo userInfo = userInfoService.findById(bl.getBlackUserId());

                userInfoList.add(userInfo);
            });
        }

        //5.给vo设置userInfoList
        vo.setItems(userInfoList);

        //6.返回vo
        return ResponseEntity.ok(vo);
    }

    public void deleteBlackList(Long blackId) {
        blackListService.deleteByUserIdAndBlackId(UserHolder.get().getId(),blackId);
    }
}
